from rest_framework.generics import ListAPIView, GenericAPIView
from rest_framework.permissions import IsAdminUser
from rest_framework.response import Response
from rest_framework.views import APIView

from goods.models import SPU, SPUSpecification
from meiduo_admin.serializers.spus import SPUSimpleSerializer, SPUSpecSerialzier


class SPUSimpleView(ListAPIView):
    # sku管理下新增商品的spu下拉栏
    pagination_class = [IsAdminUser]

    #指定视图所使用的查询集
    queryset = SPU.objects.all()
    #指定视图所使用的序列化器类
    serializer_class = SPUSimpleSerializer

    #garni分页
    pagination_class = None



class SPUSpecView(APIView):
    permission_classes = [IsAdminUser]


    def get(self,request,pk):
        """
        获取SPU 规格选项数据
        1.根据pk获取specs数据
        2.将spu数据序列化返回
        """
        #1.sku管理下新增商品的spu下拉栏
        specs = SPUSpecification.objects.filter(spu_id = pk)
        #2.将spu数据序列化返回
        serializer = SPUSpecSerialzier(specs,many=True)
        return Response(serializer.data)